package com.csist.controller;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.csist.pojo.bo.UserInfoBo;
import com.csist.pojo.po.basicInfoModule.EducationMethod;
import com.csist.pojo.po.basicInfoModule.EducationStatus;
import com.csist.pojo.po.basicInfoModule.LevelMajorInfo;
import com.csist.pojo.po.basicInfoModule.SpecificationMajorInfo;
import com.csist.pojo.po.userModule.UserInfo;
import com.csist.pojo.po.userModule.UserType;
import com.csist.service.bo.UserInfoBoService;
import com.csist.service.bo.impl.UserInfoBoServiceImpl;
import com.csist.service.po.basicInfoModule.EducationMethodService;
import com.csist.service.po.basicInfoModule.EducationStatusService;
import com.csist.service.po.basicInfoModule.LevelMajorInfoService;
import com.csist.service.po.basicInfoModule.SpecificationMajorInfoService;
import com.csist.service.po.basicInfoModule.impl.EducationMethodServiceImpl;
import com.csist.service.po.basicInfoModule.impl.EducationStatusServiceImpl;
import com.csist.service.po.basicInfoModule.impl.LevelMajorInfoServiceImpl;
import com.csist.service.po.basicInfoModule.impl.SpecificationMajorInfoServiceImpl;
import com.csist.service.po.userModule.UserInfoService;
import com.csist.service.po.userModule.UserTypeService;
import com.csist.service.po.userModule.impl.UserInfoServiceImpl;
import com.csist.service.po.userModule.impl.UserTypeServiceImpl;
import com.csist.util.JSONUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@WebServlet("/dropdownQuery")
public class DropdownQueryServlet extends HttpServlet {
    private JSONObject jsonObject;
    //    private BasicStudentInfoService basicStudentInfoService = new BasicStudentInfoServiceImpl();//学生基础信息
    private EducationStatusService educationStatusService = new EducationStatusServiceImpl();//专修在校状态
    //    private LevelRegistrationBoService levelRegistrationBoService = new LevelRegistrationBoServiceImpl();
    //    private SpecificationClassInfoBoService specificationClassInfoBoService = new SpecificationClassInfoBoServiceImpl();
    //    专修专业
    private SpecificationMajorInfoService specificationMajorInfoService = new SpecificationMajorInfoServiceImpl();
    //    用户身份
    private UserTypeService userTypeService = new UserTypeServiceImpl();
    //    用户信息
    private UserInfoService userInfoService = new UserInfoServiceImpl();
    //    教学方式
    private EducationMethodService educationMethodService = new EducationMethodServiceImpl();
    //    学历专业
    private LevelMajorInfoService levelMajorInfoService = new LevelMajorInfoServiceImpl();
    //    用户业务类
    private UserInfoBoService userInfoBoService = new UserInfoBoServiceImpl();


    //    private
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setContentType("application/json;charset=utf-8");
//        这里如果request没有json字符串，则jsonObject为null（获取不到）
        jsonObject = JSONUtil.getJSONObject(request);
        if (jsonObject == null) {
            err(request, response);
            return;
        }
        String type = jsonObject.getString("type");
        if (type != null) {
            switch (type) {
//                case "studentEducationStatus":
//                    studentEducationStatus(request, response);
//                    break;
//                case "levelRegistration":
//                    levelRegistration(request, response);
//                    break;
//                case "studentSpecificationClass":
//                    studentSpecificationClass(request, response);
//                    break;
//                case "studentSpecificationMajor":
//                    studentSpecificationMajor(request, response);
//                    break;
                case "findAllClassTeacherInfo":
                    findAllClassTeacherInfo(request, response);
                    break;
                case "findAllSpecificationMajor":
                    findAllSpecificationMajor(request, response);
                    break;
                case "findAllEducationStatus":
                    findAllEducationStatus(request, response);
                    break;
                case "findAllEducationMethod":
                    findAllEducationMethod(request, response);
                    break;
                case "findAllLevelMajorInfo":
                    findAllLevelMajorInfo(request, response);
                    break;
                case "findAll":
                    findAll(request, response);
                    break;
                default:
                    err(request, response);
            }
        } else {
            err(request, response);
        }
    }

    private void findAll(HttpServletRequest request, HttpServletResponse response) throws IOException,
            ServletException {
        Map<String, Object> queryList = new HashMap<>();
        boolean status = true;
        String msg = "";
        StringBuilder sb = new StringBuilder("");
        List<LevelMajorInfo> lmil = levelMajorInfoService.findAll();
        if (lmil == null || lmil.size() <= 0) {
//            status = false;
            sb.append("学历专业未成功获取；");
        }
        queryList.put("eml", lmil);
        List<EducationMethod> eml = educationMethodService.findAll();
        if (eml == null || eml.size() <= 0) {
//            status = false;
            sb.append("学历类型未成功获取；");
        }
        queryList.put("tml", eml);
        List<EducationStatus> esl = educationStatusService.findAll();
        if (esl == null || esl.size() <= 0) {
//            status = false;
            sb.append("在校状态未成功获取；");
        }
        queryList.put("lsl", esl);
        List<SpecificationMajorInfo> smil = specificationMajorInfoService.findAll();
        if (smil == null || smil.size() <= 0) {
//            status = false;
            sb.append("专修专业未成功获取；");
        }
        queryList.put("sml", smil);
        List<UserInfoBo> uibl =
                userInfoBoService.selectAllUserInfo().stream().filter((e) -> {
                    if (e.getUserType() != null && e.getUserType().getUtName() != null) {
                        return "班主任".equals(e.getUserType().getUtName());
                    } else {
                        return false;
                    }
                }).collect(Collectors.toList());
        queryList.put("tnl", uibl);
        if (uibl == null || uibl.size() <= 0) {
//            status = false;
            sb.append("班主任列表未成功获取；");
        }
        queryList.put("tnl", uibl);
        queryList.put("status", status);
        queryList.put("msg", status ? "查询信息成功..." : sb.toString());
        response.getWriter().write(JSON.toJSONString(queryList));
    }

    /**
     * 查询所有学历专业方式
     *
     * @param request
     * @param response
     * @throws IOException
     */
    private void findAllLevelMajorInfo(HttpServletRequest request, HttpServletResponse response) throws IOException {
        List<LevelMajorInfo> list = levelMajorInfoService.findAll();
        if (list == null) {
            findNoData(request, response);
            return;
        }
        response.getWriter().write(JSON.toJSONString(list));

    }

    /**
     * 查询所有学历教学方式
     *
     * @param request
     * @param response
     * @throws IOException
     */
    private void findAllEducationMethod(HttpServletRequest request, HttpServletResponse response) throws IOException {
        List<EducationMethod> list = educationMethodService.findAll();
        if (list == null) {
            findNoData(request, response);
            return;
        }
        response.getWriter().write(JSON.toJSONString(list));
    }

    /**
     * 查询学生专修在校状态/专本学历状态
     *
     * @param request
     * @param response
     * @throws IOException
     */
    private void findAllEducationStatus(HttpServletRequest request, HttpServletResponse response) throws IOException {
        List<EducationStatus> list = educationStatusService.findAll();
        if (list == null) {
            findNoData(request, response);
            return;
        }
        response.getWriter().write(JSON.toJSONString(list));
    }

    /**
     * 查询所有专修专业
     *
     * @param request
     * @param response
     * @throws IOException
     */
    private void findAllSpecificationMajor(HttpServletRequest request, HttpServletResponse response) throws IOException {
        List<SpecificationMajorInfo> list = specificationMajorInfoService.findAll();
        if (list == null) {
            findNoData(request, response);
            return;
        }
        response.getWriter().write(JSON.toJSONString(list));
    }

    /**
     * 查询用户身份为“班主任”的用户信息
     *
     * @param request
     * @param response
     * @throws IOException
     */
    private void findAllClassTeacherInfo(HttpServletRequest request, HttpServletResponse response) throws IOException {
        UserType userType = new UserType();
        userType.setUtName("班主任");
        List<UserType> userTypes = userTypeService.searchUserTypeByColumn(userType);
//        这里只会返回一条数据,获取到用户身份名称为“班主任”的用户身份编号
        int utId = userTypes.get(0).getUtId();
        UserInfo userInfo = new UserInfo();
        userInfo.setUiUtId(utId);
        List<UserInfo> list = userInfoService.searchUserInfoByColumn(userInfo);
        if (list == null) {
            findNoData(request, response);
            return;
        }
        response.getWriter().write(JSON.toJSONString(list));
    }

//    /**
//     * 根据专修编号查询专修专业信息
//     *
//     * @param request
//     * @param response
//     * @throws IOException
//     */
//    private void studentSpecificationMajor(HttpServletRequest request, HttpServletResponse response) throws IOException {
//        String specialtyNum = jsonObject.getJSONObject("queryGroup").getString("specialtyNum");
//        if (specialtyNum != null && !specialtyNum.isEmpty()) {
//            BasicStudentInfo bsi = new BasicStudentInfo();
//            bsi.setBsiSid(specialtyNum);
//            List<BasicStudentInfo> list = basicStudentInfoService.searchBSIByColumn(bsi);
//            if (list == null) {
//                findNoData(request, response);
//                return;
//            }
//            BasicStudentInfo bs = list.get(0);
//            System.out.println("bs:" + bs);
////            这里因为查询出来只有一组数据
//            Integer bsiSmiId = list.get(0).getBsiSmiId();
//            System.out.println("bsiEsId的数值为:" + bsiSmiId);
//            SpecificationMajorInfo specificationMajorInfoById = specificationMajorInfoService.getSpecificationMajorInfoById(bsiSmiId);
//            System.out.println("specificationMajorInfoById的值：" + specificationMajorInfoById);//问题出现在这里
//            if (specificationMajorInfoById == null) {
//                findNoData(request, response);
//                return;
//            }
//            response.getWriter().write(JSON.toJSONString(specificationMajorInfoById));
//            return;
//
//        }
//        err(request, response);
//    }
//
//    /**
//     * 根据专修编号查询专修班级一类信息
//     *
//     * @param request
//     * @param response
//     * @throws IOException
//     */
//    private void studentSpecificationClass(HttpServletRequest request, HttpServletResponse response) throws IOException {
//        String specialtyNum = jsonObject.getJSONObject("queryGroup").getString("specialtyNum");
//        if (specialtyNum != null && !specialtyNum.isEmpty()) {
//            BasicStudentInfo bsi = new BasicStudentInfo();
//            bsi.setBsiSid(specialtyNum);
//            List<BasicStudentInfo> list = basicStudentInfoService.searchBSIByColumn(bsi);
//            if (list == null) {
//                findNoData(request, response);
//                return;
//            }
////            这里因为查询出来只有一组数据
//            Integer bsiEsId = list.get(0).getBsiEsId();
//            System.out.println("bsiEsId的数值为:" + bsiEsId);
//            SpecificationClassInfoBo specificationClassInfoBo = specificationClassInfoBoService.getSpecificationClassInfoBoById(bsiEsId);
//            if (specificationClassInfoBo == null) {
//                findNoData(request, response);
//                return;
//            }
//            response.getWriter().write(JSON.toJSONString(specificationClassInfoBo));
//            return;
//
//        }
//        err(request, response);
//    }
//
//    /**
//     * 根据专修编号查询专修在校状态
//     *
//     * @param request
//     * @param response
//     * @throws IOException
//     */
//    private void studentEducationStatus(HttpServletRequest request, HttpServletResponse response) throws IOException {
////        specialtyNum
//        String specialtyNum = jsonObject.getJSONObject("queryGroup").getString("specialtyNum");
//        if (specialtyNum != null && !specialtyNum.isEmpty()) {
//            BasicStudentInfo bsi = new BasicStudentInfo();
//            bsi.setBsiSid(specialtyNum);
//            List<BasicStudentInfo> list = basicStudentInfoService.searchBSIByColumn(bsi);
//            if (list == null) {
//                findNoData(request, response);
//                return;
//            }
////            这里因为查询出来只有一组数据
//            Integer bsiEsId = list.get(0).getBsiEsId();
//            System.out.println("bsiEsId的数值为:" + bsiEsId);
//            EducationStatus educationStatus = educationStatusService.getEducationStatusById(bsiEsId);
//            if (educationStatus == null) {
//                findNoData(request, response);
//                return;
//            }
//            System.out.println("educationStatus的数值为:" + educationStatus);
//            response.getWriter().write(JSON.toJSONString(educationStatus));
//            return;
//        }
//        err(request, response);
//
//    }
//
//    /**
//     * 根据科大平台注册号查询学历注册信息
//     *
//     * @param request
//     * @param response
//     * @throws IOException
//     */
//    private void levelRegistration(HttpServletRequest request, HttpServletResponse response) throws IOException {
//        String registrationNum = jsonObject.getJSONObject("queryGroup").getString("registrationNum");
//        if (registrationNum != null && !registrationNum.isEmpty()) {
//            LevelRegistrationBo levelRegistrationBo = levelRegistrationBoService.selectLevelRegistrationBoByUestcId(registrationNum);
//            if (levelRegistrationBo == null) {
//                findNoData(request, response);
//                return;
//            }
//            System.out.println("levelRegistrationBo的数值为:" + levelRegistrationBo);
//            response.getWriter().write(JSON.toJSONString(levelRegistrationBo));
//            return;
//        }
//        err(request, response);
//
//    }

    private void err(HttpServletRequest request, HttpServletResponse response) throws IOException {
        HashMap<String, Object> map = new HashMap<>();
        map.put("status", false);
        map.put("msg", "传送了无效数据...");
        response.getWriter().write(JSON.toJSONString(map));
    }

    private void findNoData(HttpServletRequest request, HttpServletResponse response) throws IOException {
        HashMap<String, Object> map = new HashMap<>();
        map.put("status", false);
        map.put("msg", "无匹配数据...");
        response.getWriter().write(JSON.toJSONString(map));
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doGet(request, response);
    }
}
